package com.boruan.qq.zxgylibrary.service.presenter;

import android.content.Context;
import com.boruan.qq.zxgylibrary.base.BasePresenter;
import com.boruan.qq.zxgylibrary.base.BaseView;
import com.boruan.qq.zxgylibrary.service.manager.DataManager;
import com.boruan.qq.zxgylibrary.service.manager.GXDrugModule;
import com.boruan.qq.zxgylibrary.service.model.DownOrderBean;
import com.boruan.qq.zxgylibrary.service.model.OrderDetailBean;
import com.boruan.qq.zxgylibrary.service.model.PayInfoResponse;
import com.boruan.qq.zxgylibrary.service.model.SettleDefaultAddressBean;
import com.boruan.qq.zxgylibrary.service.model.ShopCarDataBean;
import com.boruan.qq.zxgylibrary.service.view.OrderView;
import com.convenient.qd.core.bean.BaseResBean;
import com.convenient.qd.core.constant.Constant;
import com.convenient.qd.core.net.BaseHttpObserver;
import com.convenient.qd.core.utils.UMEventUtil;
import com.convenient.qd.core.widget.LoadingDiaLogUtils;

import java.util.List;

/**
 * Created by 崔琦 on 2019/9/5.
 * des: 确认订单P层
 */

public class ConfirmOrderPresenter implements BasePresenter {

    private Context mContext;
    private OrderView orderView;
    private DataManager dataManager;
    /**
     * 获取购物车列表数据
     */
    private List<ShopCarDataBean.ResultBean> mShopCarDataBean;
    /**
     * 获取默认结算页收货地址
     */
    private SettleDefaultAddressBean.ResultBean mSettleDefaultAddressBean;

    public ConfirmOrderPresenter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onCreate() {
        dataManager = new DataManager(mContext);
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {
        orderView = null;
    }

    @Override
    public void pause() {

    }

    @Override
    public void attachView(BaseView view) {
        orderView = (OrderView) view;
    }

    public void getInsuranceBalance(){

    }

    public void getInsuranceRegisterUrl(){
    }

    /**
     * 获取购物车数据
     */
    public void getShopCarData(String shopId,int addrId) {
        if(addrId == -1){
            return;
        }
        orderView.showProgress();
        GXDrugModule.getInstance().getShopCarData(shopId,addrId, new BaseHttpObserver<BaseResBean<List<ShopCarDataBean.ResultBean>>>() {
            @Override
            public void onSuccess(BaseResBean<List<ShopCarDataBean.ResultBean>> shopCarDataBean) {
                mShopCarDataBean = shopCarDataBean.getResult();
                if (mShopCarDataBean != null) {
                    orderView.getShopCarDataSuccess(mShopCarDataBean);
                }
                orderView.hideProgress();
            }

            @Override
            public void onFailure(int i, String msg) {
                orderView.hideProgress();
                orderView.getShopCarDataFailed(msg);
            }
        });
    }

    /**
     * 获取结算页默认收货地址
     */
    public void getDefaultAddress(String shopId) {
        GXDrugModule.getInstance().getSettleDefaultAddress(shopId, new BaseHttpObserver<BaseResBean<SettleDefaultAddressBean.ResultBean>>() {
            @Override
            public void onSuccess(BaseResBean<SettleDefaultAddressBean.ResultBean> settleDefaultAddressBean) {
                mSettleDefaultAddressBean = settleDefaultAddressBean.getResult();
                orderView.getDefaultAddressSuccess(mSettleDefaultAddressBean);
            }

            @Override
            public void onFailure(int i, String msg) {
                orderView.getDefaultAddressFailed(msg);
            }
        });
    }

    /**
     * 用户下单
     */
    private DownOrderBean.ResultBean mDownOrderBean;

    public void userDownOrder(int creditToMoney, int deliveryType, int distance, int freight, int addressId, int paymentPrice, String shopId, List<ShopCarDataBean.ResultBean> shopCarDataBean, String remark,String mobile,String name) {

        orderView.showProgress();
        GXDrugModule.getInstance().userDownOrder(creditToMoney, deliveryType, distance, freight, addressId, paymentPrice, shopId, shopCarDataBean, remark,mobile,name, new BaseHttpObserver<BaseResBean<DownOrderBean.ResultBean>>() {
            @Override
            public void onSuccess(BaseResBean<DownOrderBean.ResultBean> orderBean) {
                mDownOrderBean = orderBean.getResult();

                if (mDownOrderBean != null) {
                    UMEventUtil.onOrderEventObject(mContext,"2001008", Constant.sceneId);
                    orderView.userDownOrderSuccess(mDownOrderBean);
                }
                orderView.hideProgress();
            }

            @Override
            public void onFailure(int i, String msg) {
                orderView.hideProgress();
                orderView.userDownOrderFailed(msg);
            }
        });
    }

    /**
     * 国信中台支付接口(获取支付参数)
     */
    private PayInfoResponse.ResultBean payParamJson;

    public void getPayParam(String tradeId, final String payWay,String pwd) {
        LoadingDiaLogUtils.showLoadingDialog(mContext);
        GXDrugModule.getInstance().getPayParam(tradeId, payWay,pwd, new BaseHttpObserver<BaseResBean<PayInfoResponse.ResultBean>>() {
            @Override
            public void onSuccess(BaseResBean<PayInfoResponse.ResultBean> bean) {
                payParamJson = bean.getResult();
                if (payParamJson != null) {
                    orderView.getPayParamsSuccess(payParamJson);
                }
            }

            @Override
            public void onFailure(int i, String msg) {
                orderView.getPayParamsFailed(msg);
            }
        });
    }

    /**
     * 获取订单详情
     * */
    public void getOrderDetailInfo(String orderId){

        GXDrugModule.getInstance().getOrderDetailInfo(orderId, new BaseHttpObserver<BaseResBean<OrderDetailBean.ResultBean>>() {
            @Override
            public void onSuccess(BaseResBean<OrderDetailBean.ResultBean> bean) {
                if (bean != null){
                    orderView.getOrderDetailSuccess(bean.getResult().getState());
                }
            }

            @Override
            public void onFailure(int status, String msg) {
                orderView.getOrderDetailFailed(msg);
            }
        });

    }
}
